

from core.himesis import Himesis, HimesisPostConditionPattern
import cPickle as pickle
from uuid import UUID

class HMoveOneInputIndirectMatchRHS(HimesisPostConditionPattern):
    def __init__(self):
        """
        Creates the himesis graph representing the AToM3 model HMoveOneInputIndirectMatchRHS.
        """
        # Flag this instance as compiled now
        self.is_compiled = True
        
        super(HMoveOneInputIndirectMatchRHS, self).__init__(name='HMoveOneInputIndirectMatchRHS', num_nodes=8, edges=[])
        
        # Add the edges
        self.add_edges([(0, 2), (2, 7), (0, 3), (3, 6), (0, 4), (4, 5), (1, 5), (7, 1)])
        # Set the graph attributes
        self["mm__"] = pickle.loads("""(lp1
S'MT_post__SimpleDSLTransMM'
p2
aS'MoTifRule'
p3
a.""")
        self["MT_action__"] = """#===============================================================================
# This code is executed after the rule has been applied.
# You can access a node labelled n matched by this rule by: PostNode('n').
# To access attribute x of node n, use: PostNode('n')['x'].
#===============================================================================

pass
"""
        self["name"] = """"""
        self["GUID__"] = UUID('28bb4840-bae9-4ba6-9eb3-0f88cee11154')
        
        # Set the node attributes
        self.vs[0]["MT_label__"] = """5"""
        self.vs[0]["mm__"] = """MT_post__MatchModel"""
        self.vs[0]["GUID__"] = UUID('a7d9f0a2-2ae5-4579-927e-72b5afc5c9a7')
        self.vs[1]["MT_label__"] = """10"""
        self.vs[1]["mm__"] = """MT_post__indirectLink_S"""
        self.vs[1]["GUID__"] = UUID('89c6a1cc-e7ce-4edf-8d41-37e2bbe21d4b')
        self.vs[2]["MT_label__"] = """6"""
        self.vs[2]["mm__"] = """MT_post__match_contains"""
        self.vs[2]["GUID__"] = UUID('a5c847eb-2e5f-4e36-9c1c-1445e5346c67')
        self.vs[3]["MT_label__"] = """7"""
        self.vs[3]["mm__"] = """MT_post__match_contains"""
        self.vs[3]["GUID__"] = UUID('35701dab-7ae4-4264-b54c-34b8440fe8ea')
        self.vs[4]["MT_label__"] = """8"""
        self.vs[4]["mm__"] = """MT_post__match_contains"""
        self.vs[4]["GUID__"] = UUID('c3e3a51f-69b2-4882-a753-15ce64e2c879')
        self.vs[5]["MT_pivotOut__"] = """element1"""
        self.vs[5]["MT_label__"] = """1"""
        self.vs[5]["MT_post__name"] = """
#===============================================================================
# You can access the value of the current node's attribute value by: attr_value.
# If the current node shall be created you MUST initialize it here!
# You can access a node labelled n by: PreNode('n').
# To access attribute x of node n, use: PreNode('n')['x'].
# Note that the attribute values are those before the match is rewritten.
# The order in which this code is executed depends on the label value
# of the encapsulating node.
# The given action must return the new value of the attribute.
#===============================================================================

return attr_value
"""
        self.vs[5]["mm__"] = """MT_post__MetaModelElement_S"""
        self.vs[5]["MT_post__classtype"] = """
#===============================================================================
# You can access the value of the current node's attribute value by: attr_value.
# If the current node shall be created you MUST initialize it here!
# You can access a node labelled n by: PreNode('n').
# To access attribute x of node n, use: PreNode('n')['x'].
# Note that the attribute values are those before the match is rewritten.
# The order in which this code is executed depends on the label value
# of the encapsulating node.
# The given action must return the new value of the attribute.
#===============================================================================

return attr_value
"""
        self.vs[5]["GUID__"] = UUID('619fab57-8d61-44b1-8973-7f7486feefca')
        self.vs[6]["MT_pivotOut__"] = """element2"""
        self.vs[6]["MT_label__"] = """2"""
        self.vs[6]["MT_post__name"] = """
#===============================================================================
# You can access the value of the current node's attribute value by: attr_value.
# If the current node shall be created you MUST initialize it here!
# You can access a node labelled n by: PreNode('n').
# To access attribute x of node n, use: PreNode('n')['x'].
# Note that the attribute values are those before the match is rewritten.
# The order in which this code is executed depends on the label value
# of the encapsulating node.
# The given action must return the new value of the attribute.
#===============================================================================

return attr_value
"""
        self.vs[6]["mm__"] = """MT_post__MetaModelElement_S"""
        self.vs[6]["MT_post__classtype"] = """
#===============================================================================
# You can access the value of the current node's attribute value by: attr_value.
# If the current node shall be created you MUST initialize it here!
# You can access a node labelled n by: PreNode('n').
# To access attribute x of node n, use: PreNode('n')['x'].
# Note that the attribute values are those before the match is rewritten.
# The order in which this code is executed depends on the label value
# of the encapsulating node.
# The given action must return the new value of the attribute.
#===============================================================================

return attr_value
"""
        self.vs[6]["GUID__"] = UUID('202dba63-3e63-4073-a642-ea7f8574aec5')
        self.vs[7]["MT_label__"] = """3"""
        self.vs[7]["MT_post__name"] = """
#===============================================================================
# You can access the value of the current node's attribute value by: attr_value.
# If the current node shall be created you MUST initialize it here!
# You can access a node labelled n by: PreNode('n').
# To access attribute x of node n, use: PreNode('n')['x'].
# Note that the attribute values are those before the match is rewritten.
# The order in which this code is executed depends on the label value
# of the encapsulating node.
# The given action must return the new value of the attribute.
#===============================================================================

return attr_value
"""
        self.vs[7]["mm__"] = """MT_post__MetaModelElement_S"""
        self.vs[7]["MT_post__classtype"] = """
#===============================================================================
# You can access the value of the current node's attribute value by: attr_value.
# If the current node shall be created you MUST initialize it here!
# You can access a node labelled n by: PreNode('n').
# To access attribute x of node n, use: PreNode('n')['x'].
# Note that the attribute values are those before the match is rewritten.
# The order in which this code is executed depends on the label value
# of the encapsulating node.
# The given action must return the new value of the attribute.
#===============================================================================

return attr_value
"""
        self.vs[7]["GUID__"] = UUID('eacad8c1-a6f6-471b-9c0b-fe8d07bd6410')

        from HMoveOneInputIndirectMatchLHS import HMoveOneInputIndirectMatchLHS
        self.pre = HMoveOneInputIndirectMatchLHS()
    
    def action(self, PostNode, graph):
        """
            Executable constraint code. 
            @param PostNode: Function taking an integer as parameter
                             and returns the node corresponding to that label.
        """
        #===============================================================================
        # This code is executed after the rule has been applied.
        # You can access a node labelled n matched by this rule by: PostNode('n').
        # To access attribute x of node n, use: PostNode('n')['x'].
        #===============================================================================
        
        pass

    def execute(self, packet, match):
        """
            Transforms the current match of the packet according to the rule %s.
            Pivots are also assigned, if any.
            @param packet: The input packet.
            @param match: The match to rewrite.
        """
        graph = packet.graph
        
        # Build a dictionary {label: node index} mapping each label of the pattern to a node in the graph to rewrite.
        # Because of the uniqueness property of labels in a rule, we can store all LHS labels
        # and subsequently add the labels corresponding to the nodes to be created.
        labels = match.copy()
        
        #===============================================================================
        # Update attribute values
        #===============================================================================
        
        #===============================================================================
        # Create new nodes
        #===============================================================================
        # indirectLink_S10
        new_node = graph.add_node()
        labels['10'] = new_node
        graph.vs[new_node][Himesis.Constants.META_MODEL] = 'indirectLink_S'
        
        #===============================================================================
        # Create new edges
        #===============================================================================
        # indirectLink_S10 -> MetaModelElement_S1
        graph.add_edges((labels['10'], labels['1']))
        # MetaModelElement_S3 -> indirectLink_S10
        graph.add_edges((labels['3'], labels['10']))
        
        #===============================================================================
        # Set the output pivots
        #===============================================================================
        # MetaModelElement_S1
        packet.global_pivots['element1'] = graph.vs[labels['1']][Himesis.Constants.GUID]
        # MetaModelElement_S2
        packet.global_pivots['element2'] = graph.vs[labels['2']][Himesis.Constants.GUID]
        
        #===============================================================================
        # Perform the post-action
        #===============================================================================
        try:
            self.action(lambda i: graph.vs[labels[i]], graph)
        except Exception, e:
            raise Exception('An error has occurred while applying the post-action', e)
        #===============================================================================
        # Finally, delete nodes (this will automatically delete the adjacent edges)
        #===============================================================================
        # MT_pre__indirectLink_S9
        graph.delete_nodes([labels["9"]])
    
